package everyday.m20230309;

import java.util.Arrays;

/**
 * leetcode 题2383
 */
public class Q20230313 {

    public static void main(String[] args) {
        Q20230313 q20230313 = new Q20230313();
        System.out.println(q20230313.minNumberOfHours(2, 4, new int[]{
                1
        }, new int[] {3}));
    }
    public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
        // 最少需要多少精力
        int needEnergy = Arrays.stream(energy).sum() + 1;
        // 最少需要多少经验,先计算前缀和
        int[] preSum = new int[experience.length + 1];
        for (int i = 0; i < experience.length; i++) {
            preSum[i + 1] = preSum[i] + experience[i];
        }
        // 再计算每次最少需要的经验数
        int needExpe = 0;
        for (int i = 0; i < experience.length; i++) {
            needExpe = Integer.max(needExpe, experience[i] - preSum[i] + 1);
        }
        return Integer.max(0,needExpe - initialExperience) + Integer.max(needEnergy - initialEnergy, 0);
    }
}
